package com.living.player.ui.player;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.living.R;
import com.living.player.event.EventStatusPause;
import com.living.player.event.EventStatusPlay;
import com.living.player.services.MediaPlayerService;
import com.living.player.widget.PHAudioView;
import com.living.utils.ImgLoadUtils;
import com.md.network.api.Audio;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;

public class PlayingFragment extends Fragment implements View.OnClickListener {

    PHAudioView video_animator;
    TextView tv_playing_title, tv_playing_abulm;
    ImageView tv_playing_add;
    private Audio curAudio;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_palyer_playing, container, false);
        video_animator = view.findViewById(R.id.video_animator);
        tv_playing_title = view.findViewById(R.id.tv_playing_title);
        tv_playing_abulm = view.findViewById(R.id.tv_playing_abulm);
        tv_playing_add = view.findViewById(R.id.tv_playing_add);
        tv_playing_add.setOnClickListener(this);

        initData();
        return view;
    }

    public void initData() {
        Intent intent = getActivity().getIntent();
        if (intent.hasExtra("audio")) {
            curAudio = (Audio) intent.getParcelableExtra("audio");
            tv_playing_title.setText(curAudio.getName() + "");
            tv_playing_abulm.setText(curAudio.getAuthor() + "");
            ImgLoadUtils.disPlayCircle(getActivity(), video_animator.getImgAudioAnimation(), curAudio.getCoverUrl(), R.drawable.icon_player_audio_pic);
        }
    }

    @Subscribe
    public void onEvent(Object event) {
        if (event instanceof EventStatusPlay) {
            video_animator.resumeMP3Animation();
        } else if (event instanceof EventStatusPause) {
            video_animator.pauseMP3Animation();
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_bootom_play_status) {
            if (MediaPlayerService.isPlaying()) {
                EventBus.getDefault().post(new EventStatusPause());
            } else if (MediaPlayerService.isPlayable()) {
                EventBus.getDefault().post(new EventStatusPlay());
            }
        } else if (v.getId() == R.id.tv_playing_add) {
            if (curAudio != null) {
                ArrayList<Audio> audioArrayList = new ArrayList<>();
                audioArrayList.add(curAudio);
                ARouter.getInstance()
                        .build("/album/albumlist")
                        .withParcelableArrayList("SELECTED_AUDIOS", audioArrayList)
                        .navigation();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }


}
